package ar.edu.utn.frsf.tpae.a2012.g04.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.edu.utn.frsf.tpae.a2012.g04.dao.PagoDao;
import ar.edu.utn.frsf.tpae.a2012.g04.dominio.Cuenta;
import ar.edu.utn.frsf.tpae.a2012.g04.dominio.Pago;

@Component
public class PagoDaoHibernate implements PagoDao {

	private SessionFactory sessionFactory;

	
	@Autowired(required = true)
	  public void setSessionFactory(SessionFactory factory) {
	    this.sessionFactory = factory;
	  }

	  private Session getCurrentSession() {
	    return sessionFactory.getCurrentSession();
	  }

	@Override
	public Pago persistir(Pago pago) {		
		if(pago.getId()==null){
			getCurrentSession().save(pago);
		}else{
			getCurrentSession().update(pago);
		}
	    return pago;
	}

	@Override
	public void eliminar(Pago pago) {
		getCurrentSession().delete(pago);
	}

	@Override
	public List<Pago> listar() {
	    Query query = getCurrentSession().createQuery("from " + Pago.class.getName());
	    return query.list();
	}

	@Override
	public Pago buscarPorId(Integer id) {
		Query query = getCurrentSession().createQuery("from " + Pago.class.getName() + " p where p.id = " + id);
	    Pago pago = (Pago) query.uniqueResult();
		return pago;
	}

	@Override
	public void realizarPago(Pago pago) {

		this.persistir(pago);
		
	}

}
